import React, { useEffect } from "react";

export default function App() {
    let inputRef = null;

    return <div>
        <button onClick={() => {
            inputRef.current.focus();
            inputRef.current.value = "";
        }}>获取焦点</button>
        <Child onFocus={(el) => inputRef = el}/>
    </div>;
}

function Child(props) {
    const inputRef = React.useRef(null);
    const {onFocus} = props;
    useEffect(() => {
        console.log(1111)
        onFocus(inputRef)
    }, [onFocus]);
    return <div>
        <input ref={inputRef} defaultValue="222"/>
    </div>
}